/*
 * @Author: your name
 * @Date: 2021-11-22 16:02:33
 * @LastEditTime: 2022-01-06 21:14:29
 * @LastEditors: your name
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \agricultural-production-web\src\api\axios.js
 */
/*
*@descrion:此封装方法主要时用户的登录，与选择农场页面
*/
import axios from "axios";
// import { Message } from "element-ui";
import { getToken } from "@/utils/auth";
import store from "@/store";
import baseUrl from "@/api/baseURL"
const zhaxios = axios.create({
  // baseURL: "http://ia.senontech.com/",

  baseURL: baseUrl.webServiceUrl,
  // baseURL: "http://192.168.1.85:9090/production/",
  // baseURL: "http://192.168.1.97:9090/",
  timeout: 40000 // 请求超时时间
});

// request interceptor
zhaxios.interceptors.request.use(
  config => {
    // 这个玩意现在登录加上去就可以登录
    config.headers["sysDataBaseName"] = getToken("databaseName") || "DB_TDW1_COMMON";
    if (getToken("token")) {
      config.headers["accessToken"] = getToken("token");
    }
    if (getToken("token")) {
      config.headers["databaseName"] = getToken("databaseName") || 'DB_TDW1_COMMON';
      config.headers["organizationList"] = getToken("organizationList") || [""];
      config.headers["organizationNo"] = getToken("organizationNo") || "";
      config.headers["systemMark"] = "ZY";
      config.headers["virtualSystemNo"] = getToken("virtualSystemNo") || 'ZY';
    }
    if (getToken("lesseeNo")) {
      config.headers["lesseeNo"] = getToken("lesseeNo");
    }
    // 导入参数设置 start
    if (config.url.includes('/downExcel') || config.url.includes('/export')) {
      // 导出文件设置请求头
      config.headers["responseType"] = "blob"
    }
    if (config.url.includes('/sheep/xlsUpdate') || config.url.includes('/import')) {
      // 导入文件设置请求头
      config.headers["breedTypeNo"] = getToken("breedTypeNo") || 'sheep'
      config.headers["Content-Type"] = "multipart/form-data"
    }
    if (config.url.includes('/web/login')) {
      config.headers["Content-Type"] = "application/x-www-form-urlencoded"
    }
    // 导入参数设置 end
    // 全局参数 start
    if (config.url.includes('/queryPageByCondition')) {
      if (config.data !== undefined) {
        if (config.data.entity !== undefined) {
          config.data.entity.breedTypeNo = getToken("breedTypeNo") || 'sheep'
          config.data.entity.organizationNo = getToken("organizationNo") || ''
          config.data.entity.systemNo = "ZY";

          config.headers["Content-Type"] = "application/json"
        }
      }
    }
    if (config.url.includes('/queryByCondition') || config.url.includes('/importPublic')) {
      if (config.data !== undefined) {
        if (!config.data?.organizationNo) config.data.organizationNo = getToken("organizationNo") || ''
        if (!config.url.includes('organizationBreedType/queryByCondition')) {
          config.data.breedTypeNo = getToken("breedTypeNo") || 'sheep'
        }
        config.data.systemNo = "ZY";
        config.headers["Content-Type"] = "application/json"
      }
    }
    if (config.url.includes('/measureRecordExport')) {
      if (config.data !== undefined) {
        if (!config.data?.organizationNo) config.data.organizationNo = getToken("organizationNo") || ''
        config.data.breedTypeNo = getToken("breedTypeNo") || 'sheep'
        config.data.systemNo = "ZY";
      }
    }
    if (config.url.includes('/add')) {
      if (config.data !== undefined) {
        if (!config.data?.organizationNo) config.data.organizationNo = getToken("organizationNo") || ''
        if (!config.data?.farmingTypeNo) config.data.farmingTypeNo = getToken("farmingTypeNo") || ''
        config.data.breedTypeNo = getToken("breedTypeNo") || 'sheep'
        config.data.systemNo = "ZY";
      }
    }
    if (config.url.includes('/exportData')) {
      if (config.data !== undefined) {
        if (!config.data?.organizationNo) config.data.organizationNo = getToken("organizationNo") || ''
        config.data.breedTypeNo = getToken("breedTypeNo") || 'sheep';
        config.data.systemNo = "ZY";
      }
    }
    if (config.url.includes('/delByNo?')) {
      config.url = config.url + '&organizationNo=' + getToken("organizationNo") || ''
    }
    if (config.url.includes('/queryByNo?')) {
      config.url = config.url + '&organizationNo=' + getToken("organizationNo") || ''
    }
    if (config.url.includes('/delListByNo')) {
      if (config.data !== undefined) {
        config.data.organizationNo = getToken("organizationNo") || ''
      }
    }
    // 全局参数 end

    config.cancelToken = new axios.CancelToken(cancel => {
      store.commit('permission/add_AxiosPromiseArr', { cancel })
    })
    return config;
  },
  error => {
    console.log(error);
    return Promise.reject(error);
  }
);

// response interceptor
zhaxios.interceptors.response.use(
  response => {
    const res = response.data;
    return res;
  },
  error => {
    console.log("err" + error);
    // Message({
    //   message: error.message,
    //   type: "error",
    //   duration: 5 * 1000
    // });
    return Promise.reject(error);
  }
);

// 封装get请求
export function get(url, params = {}) {
  params.t = new Date().getTime(); // get方法加一个时间参数,解决ie下可能缓存问题.
  console.log(url, 'get', params)
  return zhaxios({
    url: url,
    method: "get",
    params
  });
}

// 封装post请求
export function post(url, data = {}) {
  // 默认配置
  let sendObject = {
    url: url,
    method: "post",
    data
  };
  console.log(url, 'post', data)

  sendObject.data = JSON.stringify(data);
  return zhaxios(sendObject);
}

export default zhaxios;
